<template>
	<view>
		<!--<import src="/remao_shop/component/noData/noData"></import>-->
		<view class="title">
			<view :class="index == 1 ? 'act' : ''" @tap="changeIndex">
				<text>收入</text>
			</view>
			<view :class="index == 2 ? 'act' : ''" @tap="changeIndex1">
				<text>支出</text>
			</view>
		</view>
		<view class="main" v-if="index == 1">
			<view v-for="(item, index) in list" :key="index" class="item">
				<view class="one">
					<text>{{item.reason}}</text>
					<text>+{{item.integral}}</text>
				</view>
				<view class="two">
					<text>{{item.add_time}}</text>
				</view>
			</view>
			<no-data v-if="show1"></no-data>
			<text v-if="!show1">{{msg}}</text>
		</view>
		<view class="main" v-if="index == 2">
			<view v-for="(item, index) in list" :key="index" class="item">
				<view class="one">
					<text>{{item.reason}}</text>
					<text>-{{item.integral}}</text>
				</view>
				<view class="two">
					<text>{{item.add_time}}</text>
				</view>
			</view>
			<no-data v-if="show2"></no-data>
			<text v-if="!show2">{{msg}}</text>
		</view>
	</view>
</template>

<script>
	import api from "../../../api";
	var app = getApp();
	import noData from '../../component/noData/noData.vue'
	export default {
		components: {
			noData
		},
		data() {
			return {
				user_id: '',
				index: 1,
				page: 1,
				list: [],
				msg: '',
				show1: false,
				show2: false
			};
		},

		components: {},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			wx.getStorage({
				key: 'userId',
				success: res => {
					console.log(res);
					this.setData({
						user_id: res.data
					});
					this.getList(1);
				}
			});
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			var index = this.index;
			this.setData({
				page: 1
			});
			console.log(index);

			if (index == 1) {
				this.getList(1);
			} else {
				this.getList(2);
			}
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			var index = this.index;
			var page = this.page;
			console.log(page);
			this.setData({
				page: ++page
			});
			console.log(this.page);
			console.log(index);

			if (index == 1) {
				this.getList(1);
			} else {
				this.getList(2);
			}
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			getList: function(type) {
				console.log('积分类型' + type);
				var that = this;
				app.globalData.request({
					url: api.default.integral_number,
					data: {
						uid: that.user_id,
						type: type,
						page: that.page
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: 'POST',
					success: res => {
						wx.stopPullDownRefresh();
						wx.hideLoading();
						console.log(res);
						that.setData({
							list: res.integral_list
						});

						if (this.page == 1) {
							if (this.list.length == 0) {
								if (type == 1) {
									this.setData({
										show1: true
									});
								} else {
									this.setData({
										show2: true
									});
								}
							} else if (this.list.length == 10) {
								this.setData({
									msg: '加载更多数据'
								});
							} else {
								this.setData({
									msg: '已加载全部数据'
								});
							}
						} else {
							if (this.list.length == 10) {
								this.setData({
									msg: '加载更多数据'
								});
							} else {
								this.setData({
									msg: '已加载全部数据'
								});
							}
						}
					}
				});
			},
			changeIndex: function(e) {
				wx.showLoading();
				this.setData({
					index: 1,
					page: 1
				});
				this.getList(1);
			},
			changeIndex1: function(e) {
				wx.showLoading();
				this.setData({
					index: 2,
					page: 1
				});
				this.getList(2);
			}
		}
	};
</script>
<style>
	@import "./number.css";
</style>
